package com.hhkj.admin.order.service.extend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.admin.order.common.cont.OrderItemConst;
import com.hhkj.admin.order.common.cont.OrderRefundConst;
import com.hhkj.admin.order.domain.OrderItem;
import com.hhkj.admin.order.domain.OrderRefundItemAsso;
import com.hhkj.admin.order.model.dopo.OrderItemDo;
import com.hhkj.admin.order.model.dopo.OrderItemPo;
import com.hhkj.admin.order.service.base.OrderItemService;
import com.hhkj.admin.order.service.base.OrderRefundItemAssoService;
import com.hhkj.admin.order.service.extend.OrderItemExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderItemExtendServiceImpl
        extends BaseExtendServiceImpl<OrderItem, OrderItemDo, OrderItemPo, OrderItemService>
        implements OrderItemExtendService {

    @Autowired
    private OrderRefundItemAssoService orderRefundItemAssoService;

    @Override
    public List<OrderItemPo> listPoByOrderIdList(List<Long> orderIdList) {
        OrderItemDo conditionDo = new OrderItemDo();
        conditionDo.setOrderIdList(orderIdList);
        List<OrderItemPo> orderItemPoList = this.getService().listPoByDo(conditionDo);
        return orderItemPoList;
    }

    @Override
    public boolean cancelRefundByIdList(List<Long> idList) {
        LambdaUpdateWrapper<OrderItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(OrderItem::getId, idList)
                .set(OrderItem::getRefundStatusCode, OrderRefundConst.StatusEnum.CANCEL.getCode());
        return this.getService().update(updateWrapper);
    }

    @Override
    public List<OrderItem> listPoByRefundId(Long refundId) {
        LambdaQueryWrapper<OrderRefundItemAsso> queryWrapper = new LambdaQueryWrapper<OrderRefundItemAsso>()
                .eq(OrderRefundItemAsso::getRefundId, refundId);
        List<Long> orderItemIdList =
                orderRefundItemAssoService.list(queryWrapper).stream().map(OrderRefundItemAsso::getOrderItemId).collect(Collectors.toList());
        return this.getService().listByIdList(orderItemIdList);
    }
}
